package DesignPatern.A07_原型模式.QSB.A01基本模式;

import java.util.Random;

/**
 * @Author: Du
 * @Date: 2021/7/22 11:48
 *
 * 单线程发送大量邮件时花费时间较长，多线程发送可能出现线程安全问题
 * 所以解决方法之一就是采用原型模式，复制类，达到折中的效果
 */
public class A01_Main
{
    private static int MAX_COUNT = 6;
    
    public static void main(String[] args)
    {
        int i = 0;
        Mail mail = new Mail(new AdvTemplate());
        mail.setTail("XX银行版权所有");
        while (i < MAX_COUNT)
        {
            Mail cloneMail = mail.clone();//复制
            cloneMail.setApplication(getRandString(5) + "先生(女士)");
            cloneMail.setReceiver(getRandString(5) + "@" + getRandString(8));
            sendMail(cloneMail);
            i++;
        }
    }
    
    public static void sendMail(Mail mail)
    {
        System.out.println("标题" + mail.getSubject() + "收件人" + mail.getReceiver() + "发送成功");
    }
    
    public static String getRandString(int maxLen)
    {
        String source = "abcdefghijklmnopqrstuvwxyz";
        StringBuilder sb = new StringBuilder();
        Random rand = new Random();
        for (int i = 0; i < maxLen; i++)
        {
            sb.append(source.charAt(rand.nextInt(source.length())));
        }
        return sb.toString();
    }
}
